package com.lge.qcontact.threads;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.lge.qcontact.utils.LocalDBHelper;
import com.lge.qcontact.utils.MUtils;

public class SyncronizeContactsThread extends Thread {

	private ArrayList<String> gNameList;
	
	private SQLiteDatabase localDB;
	private LocalDBHelper lHelper;
	private Context mContext;
	
	private boolean runSQL = false;
	
	public SyncronizeContactsThread(Context c){
		this.mContext = c;

		gNameList = new ArrayList<String>();
	}
		
	@Override
	public void run() {
		MUtils.loge("syncThread Started");
		
		String uid = null;
		String name = null;
		String email = null;
		String mobile = null;
		String phone = null;
		String gName = null;
		String isMaster = null;
		String sql = null;
		
		
		lHelper = new LocalDBHelper(mContext);
		localDB = lHelper.getWritableDatabase();
		
		Cursor c = localDB.rawQuery("select * from LOGIN_INFO;", null);
		c.moveToNext();
		String uId = c.getString(0);
		
		try {
			URL url = new URL(
					"http://192.168.0.205:8080/com.lg.project.myexample.cmd.RetrieveCmd.vcc?param=user_info,uid,"+uId);
			
			XmlPullParserFactory factory = XmlPullParserFactory
					.newInstance();
			XmlPullParser parser = factory.newPullParser();
			parser.setInput(url.openStream(), "utf-8");

			int eventType = parser.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				switch (eventType) {
				case XmlPullParser.START_DOCUMENT:
					break;
				case XmlPullParser.START_TAG:
					if (parser.getName().equals("gname")) gNameList.add(parser.nextText());
					break;
				case XmlPullParser.END_TAG:
					break;
				}
				eventType = parser.next();
			}
			
			MUtils.log("gNameSize", ""+gNameList.size());
			
			
			for(int i=0; i<gNameList.size(); i++){
				gName = gNameList.get(i);
				MUtils.log("gNames", gName);
				
				url = new URL("http://192.168.0.205:8080/com.lg.project.myexample.cmd.RetrieveMemberUserJoinCmd.vcc?" +
									  "param="+gName);
				factory = XmlPullParserFactory.newInstance();
				parser = factory.newPullParser();
				parser.setInput(url.openStream(), "utf-8");

				eventType = parser.getEventType();
				while (eventType != XmlPullParser.END_DOCUMENT) {
					switch (eventType) {
					case XmlPullParser.START_DOCUMENT:
						break;
					case XmlPullParser.START_TAG :
						if (parser.getName().equals("uid")) uid = parser.nextText();
						if (parser.getName().equals("uname")) name = parser.nextText();
						if (parser.getName().equals("email")) email = parser.nextText(); 
						if (parser.getName().equals("mobile")) mobile = parser.nextText();
						if (parser.getName().equals("phoneno")) phone = parser.nextText();
						if (parser.getName().equals("ismaster")) isMaster = parser.nextText();
						if(isMaster != null) runSQL = true;
						break;
					case XmlPullParser.END_TAG:
						break;
					}
					eventType = parser.next();
					if(runSQL){
						c = localDB.rawQuery("select * from CONTACTS where uId='" + uid + "' and gname='" + gName + "';", null);
						if(c.getCount()>0){
							sql = "update CONTACTS " +
									"set gName='" + gName + "'," +
									"uId='" + uid + "'," + 
									"uName='" + name + "'," +
									"phoneNo='" + phone + "'," +
									"mobile='" + mobile + "'," +
									"email='" + email + "'," +
									"isMaster='" + isMaster + "' " +
									"where uId='" + uid +"' and gName='"+ gName +"';";
						}else{
							sql = "insert into CONTACTS (gName, uId, uName, phoneNo, mobile, email, isMaster) " +
									"values ('" + gName + "'," + "'" + uid + "','" + name + "'," + "'" + phone + "'," + "'" + mobile + "'," +
												"'" + email + "'," + "'" + isMaster + "');";
						}
						
						MUtils.loge("sql="+ sql);
						
						localDB.execSQL(sql);
						runSQL = false;
						isMaster = null;
					}
					
					
				}
			}
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		c.close();
		localDB.close();
		
		MUtils.loge("syncThread finished.");
	}
	
	
	
	
	
	
}
